package pswvclass.pswvandroid2.com.more.Wallet;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.EnvUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.PswvActivity;
import pswvclass.pswvandroid2.com.Register;
import pswvclass.pswvandroid2.com.more.MoreMenu;
import pswvclass.pswvandroid2.com.user.FindAccountPassword;
import pswvclass.pswvandroid2.com.user.SafeSetInner;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 */
public class Wallet extends AppCompatActivity implements View.OnClickListener{
    private String tag = "Wallet";
    private View wallet_money1;
    private View wallet_recharge;
    private View wallet_ybcoin1;
    private View wallet_income1;
    private View set_pay_password;
    private TextView wallet_money;
    private TextView wallet_ybcoin;
    private TextView wallet_income;
    protected static final int NULL_SUCCESS =2;
    protected static final int SUCCESS =1;
    protected static final int ERROR = 0;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.pswv_more_wallet);
        wallet_money1 = findViewById(R.id.wallet_money1);
        wallet_money1.setOnClickListener(this);
        wallet_recharge = findViewById(R.id.wallet_recharge);
        wallet_recharge.setOnClickListener(this);
        wallet_ybcoin1 = findViewById(R.id.wallet_ybcoin1);
        wallet_ybcoin1.setOnClickListener(this);
        wallet_income1 = findViewById(R.id.wallet_income1);
        wallet_income1.setOnClickListener(this);
        set_pay_password = findViewById(R.id.set_pay_password);
        set_pay_password.setOnClickListener(this);
        wallet_money= (TextView) findViewById(R.id.wallet_money);
        wallet_ybcoin= (TextView) findViewById(R.id.wallet_ybcoin);
        wallet_income= (TextView) findViewById(R.id.wallet_income);
        DBHelper db = new DBHelper(this);
        SQLiteDatabase sd = db.getWritableDatabase();
        int userId = 0;
        Cursor cursor = sd.query("user", new String[]{"userId"}, "state=?", new String[]{"1"}, null, null, null);
        while (cursor.moveToNext()) {
            userId = cursor.getInt(0);
        }
        cursor.close();
        final int finalUserId = userId;
        new Thread() {
            public void run() {
                try {
                    HttpUtils hu = new HttpUtils();
                    String data2;
                    final String src = getResources().getString(R.string.src_mysql) + "pswv/api/myWalletAction/queryMyWalletById";
                    final Map<String, String> map1 = new HashMap<String, String>();
                    map1.put("userId", finalUserId + "");
                    data2 = hu.submitPostData(src, map1, "utf-8");
                    JSONObject jsonObj = new JSONObject(data2);
                    JSONObject jsonObjt = jsonObj.getJSONObject("response");
                    Log.v(tag,jsonObjt+"");
                    JSONArray array = jsonObjt.getJSONArray("list");
                    int result = Integer.parseInt(jsonObj.getString("code"));
                    if (result == 0) {
                        if(array.length()==0){
                            Message msg = Message.obtain();
                            msg.what = NULL_SUCCESS;
                            mHandler.sendMessage(msg);
                        }else {
                            Message msg = Message.obtain();
                            msg.obj=array.get(0);
                            msg.what = SUCCESS;
                            mHandler.sendMessage(msg);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = ERROR;
                    mHandler.sendMessage(msg);
                    Log.v(tag, e + "");
                }
            }
        }.start();
    }
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NULL_SUCCESS: {
                        wallet_money.setText("0.00");
                        wallet_ybcoin.setText("0.00");
                        wallet_income.setText("0.00");
                }
                    break;
                case SUCCESS: {
                    JSONObject jsonObj = (JSONObject) msg.obj;
                    try {
                        wallet_money.setText(jsonObj.getString("money"));
                        wallet_ybcoin.setText(jsonObj.getString("ybCion"));
                        wallet_income.setText(jsonObj.getString("income"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                    break;
                case ERROR:{

                }
                    break;
                default:{

                }
                    break;
            }

        };
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //我的余额
            case R.id.wallet_money1:{
                view.setFocusableInTouchMode(true);
                Intent intent = new Intent();
                intent.setClass(this, Money.class);
                startActivity(intent);
            }
                break;
            case R.id.wallet_recharge:{

            }
                break;
            case R.id.wallet_income1:{

            }
                break;
            case R.id.wallet_ybcoin1:{

            }
                break;
            case R.id.set_pay_password:{

            }
                break;
            default:{

            }
                break;
        }
    }
    @Override
    public void onResume() {
        super.onResume();
       View cv = getWindow().getDecorView();
        cv.setFocusableInTouchMode(true);
        cv.requestFocus();
        cv.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_BACK) {
                    Intent intent = new Intent();
                    intent.putExtra("id",4);
                    intent.setClass(Wallet.this, PswvActivity.class);
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });
    }
}
